package com.ldj.hibernate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.ldj.struts.form.PersionFileForm;

public class PersionFileDAO extends BaseHibernateDAO {
	public PersionFileDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(PersionFileDAO.class);
	
	public List findAllPage(PersionFileForm pForm,String code) {
		try {	
			String	queryString = "from PersionFile where perFileDelOper='0' and perFileOrganCode='"+code+"'";
			if(pForm.getPerFileName() != null && !"".equals(pForm.getPerFileName())){
				queryString += " and perFileName like '%" + pForm.getPerFileName() + "%'";
			}
			if(pForm.getPerFileEnfoceCardNum() != null && !"".equals(pForm.getPerFileEnfoceCardNum())){
				queryString += " and perFileEnfoceCardNum like '%" + pForm.getPerFileEnfoceCardNum()+ "%'";
			}
			if(pForm.getPerFileDuty() != null && !"".equals(pForm.getPerFileDuty())){
				queryString += " and perFileDuty='" + pForm.getPerFileDuty()+ "'";
			}
			if(pForm.getPerFileSupCardNum() != null && !"".equals(pForm.getPerFileSupCardNum())){
				queryString += " and perFileSupCardNum like '%" + pForm.getPerFileSupCardNum()+ "%'";
			}
			queryString +="order by perFileName";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(8);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAll(PersionFileForm pForm,String code) {
		try {	
			String	queryString = "from PersionFile where perFileDelOper='0' and perFileOrganCode='"+code+"'";
			if(pForm.getPerFileName() != null && !"".equals(pForm.getPerFileName())){
				queryString += " and perFileName like '%" + pForm.getPerFileName() + "%'";
			}
			if(pForm.getPerFileEnfoceCardNum() != null && !"".equals(pForm.getPerFileEnfoceCardNum())){
				queryString += " and perFileEnfoceCardNum like '%" + pForm.getPerFileEnfoceCardNum()+ "%'";
			}
			if(pForm.getPerFileDuty() != null && !"".equals(pForm.getPerFileDuty())){
				queryString += " and perFileDuty='" + pForm.getPerFileDuty()+ "'";
			}
			if(pForm.getPerFileSupCardNum() != null && !"".equals(pForm.getPerFileSupCardNum())){
				queryString += " and perFileSupCardNum like '%" + pForm.getPerFileSupCardNum()+ "%'";
			}
			queryString +="order by perFileName";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	public PersionFile findById(java.lang.String id) {
		log.debug("getting PersionFile instance with id: " + id);
		try {
			PersionFile instance = (PersionFile) getSession().get(
					"com.ldj.hibernate.PersionFile", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void save(PersionFile transientInstance) {
		log.debug("saving PersionFile instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public PersionFile merge(PersionFile detachedInstance) {
		try {
			PersionFile result = (PersionFile) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void delete(PersionFile persistentInstance) {
		log.debug("deleting PersionFile instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public List findByUserIds(String uids) {
		try {
			Query queryObject=null;
				String queryString = "from PersionFile where perFileDelOper='0' and perFileId in (" + uids + ") order by perFileId";
				queryObject = getSession().createQuery(queryString);
			//return queryObject.list();
				this.setPageSize(12);
				return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findByLoginName(String loginName) {
		try {
			Query queryObject=null;
				String queryString = "from PersionFile where perFileLoginName='" + loginName + "'";
				queryObject = getSession().createQuery(queryString);
				return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	//机构范围
	 public List findBySuperviseOrganId(String pid) {
			log.debug("getting UserManagerScope instance with pid: " + pid);
			try {
				String queryString = "from UserManagerScope where userManagerScopeUserId = '" + pid + "'";
				Query queryObject = getSession().createQuery(queryString);
				return queryObject.list();
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
}